package com.tpshion.chat.service.impl;

import com.tpshion.chat.domain.dto.ReqFriendDto;
import com.tpshion.chat.domain.entity.Friend;
import com.tpshion.chat.domain.entity.ReqFriend;
import com.tpshion.chat.domain.vo.ReqFriendVo;
import com.tpshion.chat.exception.MyException;
import com.tpshion.chat.exception.RespStatus;
import com.tpshion.chat.mapper.FriendMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tpshion.chat.mapper.ReqFriendMapper;
import com.tpshion.chat.service.ReqFriendService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ReqFriendServiceImpl implements ReqFriendService{

    @Resource
    private ReqFriendMapper reqFriendMapper;

    @Resource
    private FriendMapper friendMapper;

    @Override
    @Transactional
    public void addFriend(ReqFriend reqFriend) {
        ReqFriend chexkExist = reqFriendMapper.checkExist(reqFriend);
        if(chexkExist != null){
            throw new MyException(RespStatus.REQUEST_ADD_FRIEND);
        }
        reqFriendMapper.insert(reqFriend);
    }

    /**
     * 根据用户id查询好友请求列表
     * @param userId
     * @return
     */
    @Override
    public List<ReqFriendVo> getListByUserId(String userId) {
        return reqFriendMapper.getListByUserId(userId);
    }

    /**
     * 修改好友请求状态
     * @param reqFriendDto
     */
    @Override
    @Transactional
    public void updateStatus(ReqFriendDto reqFriendDto) {
        if (reqFriendDto.getStatus() == 2){
            Friend friend = new Friend();
            friend.setUserId(reqFriendDto.getFriendId());
            friend.setFriendId(reqFriendDto.getUserId());
            friendMapper.addFriend(friend);
            Friend friendMy = new Friend();
            friendMy.setUserId(reqFriendDto.getUserId());
            friendMy.setFriendId(reqFriendDto.getFriendId());
            friendMapper.addFriend(friendMy);
        }
        reqFriendMapper.updateStatus(reqFriendDto);
    }
}
